package com.waterstudio.filedownloader.ext.m3u8.utils;

import android.support.annotation.NonNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;


/**
 * ================================================
 * 作    者：JayGoo
 * 版    本：
 * 创建日期：2017/11/18
 * 描    述: 工具类
 * ================================================
 */

public class MUtils {
    private static final String M3U8_EXT = "m3u8";

    public static boolean isM3u8Url(@NonNull String url) {
        return FilenameUtils.getExtension(URI.create(url).getPath()).equalsIgnoreCase(M3U8_EXT);
    }

    public static String getM3u8TsDir(@NonNull String m3u8Path) {
        String tsDownloadDir = new File(m3u8Path).getParent() + File.separator + FilenameUtils.getBaseName(m3u8Path) + "_ts";
        File dir = new File(tsDownloadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dir.getAbsolutePath();
    }

    public static String getM3u8TsMergePath(@NonNull String path) {
        return new File(path).getParent()
                + File.separator
                + FilenameUtils.getBaseName(path) + ".ts";
    }

    public static String getM3u8TsMergeTmpPath(@NonNull String path) {
        return new File(path).getParent()
                + File.separator
                + FilenameUtils.getBaseName(path) + ".tmp";
    }


    /**
     * 清空文件夹
     */
    public static boolean clearDir(File dir) {
        if (dir.exists()) {// 判断文件是否存在
            if (dir.isFile()) {// 判断是否是文件
                return dir.delete();// 删除文件
            } else if (dir.isDirectory()) {// 否则如果它是一个目录
                File[] files = dir.listFiles();// 声明目录下所有的文件 files[];
                for (int i = 0; i < files.length; i++) {// 遍历目录下所有的文件
                    clearDir(files[i]);// 把每个文件用这个方法进行迭代
                }
                return dir.delete();// 删除文件夹
            }
        }
        return true;
    }


    private static float KB = 1024;
    private static float MB = 1024 * KB;
    private static float GB = 1024 * MB;

    /**
     * 格式化文件大小
     */
    public static String formatFileSize(long size) {
        if (size >= GB) {
            return String.format("%.1f GB", size / GB);
        } else if (size >= MB) {
            float value = size / MB;
            return String.format(value > 100 ? "%.0f MB" : "%.1f MB", value);
        } else if (size >= KB) {
            float value = size / KB;
            return String.format(value > 100 ? "%.0f KB" : "%.1f KB", value);
        } else {
            return String.format("%d B", size);
        }
    }

    /**
     * 生成本地m3u8索引文件，ts切片和m3u8文件放在相同目录下即可
     *
     * @param m3u8Dir
     * @param m3U8
     */
//    public static File createLocalM3U8(File m3u8Dir, String fileName, M3U8 m3U8) throws IOException {
//        return createLocalM3U8(m3u8Dir, fileName, m3U8, null);
//    }

    /**
     * 生成AES-128加密本地m3u8索引文件，ts切片和m3u8文件放在相同目录下即可
     *
     * @param m3u8Dir
     * @param m3U8
     */
//    public static File createLocalM3U8(File m3u8Dir, String fileName, M3U8 m3U8, String keyPath) throws IOException {
//        File m3u8File = new File(m3u8Dir, fileName);
//        BufferedWriter bfw = new BufferedWriter(new FileWriter(m3u8File, false));
//        bfw.write("#EXTM3U\n");
//        bfw.write("#EXT-X-VERSION:3\n");
//        bfw.write("#EXT-X-MEDIA-SEQUENCE:0\n");
//        bfw.write("#EXT-X-TARGETDURATION:13\n");
//        for (M3U8Ts m3U8Ts : m3U8.getTsArrayList()) {
//            if (keyPath != null) bfw.write("#EXT-X-KEY:METHOD=AES-128,URI=\"" + keyPath + "\"\n");
//            bfw.write("#EXTINF:" + m3U8Ts.getSeconds() + ",\n");
//            bfw.write(m3U8Ts.obtainEncodeTsFileName());
//            bfw.newLine();
//        }
//        bfw.write("#EXT-X-ENDLIST");
//        bfw.flush();
//        bfw.close();
//        return m3u8File;
//    }
    public static byte[] readFile(String fileName) throws IOException {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        int length = fis.available();
        byte[] buffer = new byte[length];
        fis.read(buffer);
        fis.close();
        return buffer;
    }

    public static void saveFile(byte[] bytes, String fileName) throws IOException {
        File file = new File(fileName);
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }


}
